import { apiUrl } from '@/api/request'
import { submitReq } from '@/api/task/ideaShare.service'
import { showText } from '../../../utils/util';

Page({
  data: {
    imgUri: getApp().globalData.imgUri,
    platformList: [
      { key: 'xhs', name: '小红书', shareTip: "点击右上角「···」获取复制链接", required: { publishUrl: true } },
      { key: 'weibo', name: '微博', shareTip: "点击右上角「···」获取复制链接", required: { publishUrl: true } },
      { key: 'wechat', name: '微信', required: {} },
      { key: 'douyin', name: '抖音', shareTip: "点击右方「转发键 」获取复制链接", required: { publishUrl: true } }],

    /** 弹窗是否展示 */
    isExampleShow: false,
    isSuccessTipShow: false,
    exampleType: 1, //1上传示例 2分享示例

    /** picker字符串数组数据 */
    options: [],

    /** 原始表单数据 非提交格式 */
    formData: {
      imgsRaw: [],
      platformNum: '',
      publishUrl: ''
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(
      {
        options: this.data.platformList.map(item => item.name)
      }
    )
  },

  chooseImg() {
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);

        this.setData({
          formData: {
            ...this.data.formData,
            imgsRaw: tempFilePaths
          }
        })
      }
    });
  },



  onPickerChange(e) {
    console.log(e);
    this.setData({
      formData: {
        ...this.data.formData,
        platformNum: e.detail.value
      }
    })
  },

  onInputChange(e) {
    this.data.formData.publishUrl = e.detail.value;
  },

  clearForm() {
    this.setData({
      formData: {
        imgsRaw: [],
        platformNum: '',
        publishUrl: ''
      }
    })
  },

  onSubmit() {
    if (this.submitLock) return;
    this.submitLock = true;
    const formData = this.data.formData;
    // console.log("formData", formData);
    if (!formData.platformNum) {
      showText("请选择平台");
      this.submitLock = false;
    }
    else {
      const platformItem = this.data.platformList[formData.platformNum];
      if (!formData.imgsRaw?.length) {
        showText("请上传截图");
        this.submitLock = false;
      } else if (platformItem.required.publishUrl && (!formData.publishUrl?.trim())) {
        showText("请输入发布链接");
        this.submitLock = false;
      } else {
        wx.showLoading();
        wx.uploadFile({
          filePath: this.data.formData.imgsRaw[0],
          name: 'file',
          header: {
            'content-type': 'application/json',
            'Authorization': wx.getStorageSync('access_token')
          },
          url: apiUrl + '/file/uploadFile',
          success: (res) => {
            let res1 = JSON.parse(res.data)

            const submitData = {
              platform: platformItem.name,
              publishUrl: platformItem.required.publishUrl ? formData.publishUrl : '',
              imgs: getApp().globalData.cdn + res1.data.url
            }

            console.log("submit", this.data.formData);
            submitReq(submitData).then(res => {
              console.log(submitData, res);
              if (res.code == 200) {
                this.showSuccessTip();
              }
              else showText(res.msg);
              wx.hideLoading();
              this.submitLock = false;
            })
          },
          fail: (err) => {
            console.error(err);
            showText("上传出错");
            wx.hideLoading();
            this.submitLock = false;
          },
        })
      }
    }
  },

  onCheck() {
    wx.navigateTo({ url: '/pages/ideaShare/audit/audit' })
  },

  showExample(e) {
    this.setData({ isExampleShow: true, exampleType: e.currentTarget.dataset.type });
  },

  hideExample() {
    this.setData({ isExampleShow: false });
  },

  showSuccessTip() {
    this.clearForm();
    this.setData({ isSuccessTipShow: true });
  },

  hideSuccessTip() {
    this.setData({ isSuccessTipShow: false });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.hideExample();
    this.hideSuccessTip();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})